package cn.baiweigang.uu.strategy.future;

import cn.baiweigang.uu.enums.PeriodEnum;
import cn.baiweigang.uu.model.BasisInfo;
import cn.baiweigang.uu.utils.DateUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedList;

public class BasisLineList<E> extends LinkedList {
    private PeriodEnum periodEnum;//周期
    private BasisInfo max;//基差最大值
    private BasisInfo min;//基差最小值

    private long avgVol;//周期内 平均基差值
    private long swingVol;//周期内  波动量 最大-最小

    /**
     * 只能用此方法添加
     *
     * @param e
     * @return
     */
    @Override
    public boolean add(Object e) {
        boolean result = super.add(e);
//        //更新下最新加入的
        calculate(getLast());
        return result;
    }

    public void refreshAll() {
        swingVol = max.getBasis().subtract(min.getBasis()).longValue();
        BigDecimal tmp = new BigDecimal("0");
        for (Object obj : toArray()) {
            BasisInfo basisInfo = (BasisInfo) obj;
            tmp = tmp.add(basisInfo.getBasis());
        }
        avgVol = (long) (tmp.doubleValue() / toArray().length);
        periodEnum = max.getPeriod();
    }

    private void calculate(BasisInfo basisInfo) {
        basisInfo.setDateTime(DateUtil.formatDateYMDHMS(new Date(basisInfo.getId() * 1000)));
        if (max == null) max = basisInfo;
        if (min == null) min = basisInfo;
        if (basisInfo.getBasis().doubleValue() > max.getBasis().doubleValue()) max = basisInfo;
        if (basisInfo.getBasis().doubleValue() < min.getBasis().doubleValue()) min = basisInfo;
    }

    @Override
    public BasisInfo getFirst() {
        return (BasisInfo) super.getFirst();
    }

    @Override
    public BasisInfo getLast() {
        return (BasisInfo) super.getLast();
    }

    @Override
    public BasisInfo get(int index) {
        return (BasisInfo) super.get(index);
    }

    public PeriodEnum getPeriodEnum() {
        return periodEnum;
    }

    public BasisInfo getMax() {
        return max;
    }

    public BasisInfo getMin() {
        return min;
    }

    public long getAvgVol() {
        return avgVol;
    }

    public long getSwingVol() {
        return swingVol;
    }
}
